//
//  ProductPlanViewController.m
//
//
//  Created by mac on 15/1/11.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ProductPlanViewController.h"
#import "ProductPlanCell.h"
#import "PlanEditViewController.h"
#import "ZuzhiDetail.h"

@interface ProductPlanViewController ()

@end

@implementation ProductPlanViewController

- (IBAction)iconClick:(id)sender {
    
    ZuzhiDetail *zz = [[ZuzhiDetail alloc]init];
    zz.number = @"小农";
    [self.navigationController pushViewController:zz animated:YES];
    
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.dataList = [[NSMutableArray alloc] initWithCapacity:10];
        //        self.sectionNameList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"生产计划清单";
    [self initViews];
    [self registerNib];
}

- (void)initViews
{
    self.tblData.dataSource = self;
    self.tblData.delegate = self;
    [self setNavBarButton];
//    [self setViewHidden];
}

- (void)setViewHidden
{
    if (self.dataList.count == 0)
    {
        self.ivNoData.hidden = NO;
        self.viewPlan.hidden = YES;
    }
    else
    {
        self.ivNoData.hidden = YES;
        self.viewPlan.hidden = NO;
    }
}

- (void)registerNib
{
    UINib *nib = [UINib nibWithNibName:@"ProductPlanCell" bundle:nil];
    [self.tblData registerNib:nib forCellReuseIdentifier:@"ProductPlanCell"];
}

- (void)setNavBarButton
{
    CGRect backframe = CGRectMake(0, 0, 54, 30);
    UIButton *button = [[UIButton alloc] initWithFrame:backframe];
    [button setTitle:@"新建" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doClickNew) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItems = @[ rightItem ];
    
    
}

- (void)doClickNew
{
    [self.dataList addObject:@"new"];
    PlanEditViewController *viewController = [[PlanEditViewController alloc] initWithNibName:@"PlanEditViewController" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
//    [self setViewHidden];
    [self.tblData reloadData];
}

#pragma mark 排序 当移动了某一行时候会调用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.dataList.count + 2;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 78; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductPlanCell"];

    [cell.btnView addTarget:self action:@selector(doViewPlan) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)doViewPlan
{
    PlanEditViewController *viewController = [[PlanEditViewController alloc] initWithNibName:@"PlanEditViewController" bundle:nil];
    viewController.isViewPlan = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 32;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view;
//    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 32)];
//    view.backgroundColor = [LPCTool colorWithHexString:COLOR_BUTTON_MAIN_N];
//    UILabel *label;
//    label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width - 10, 32)];
//    label.font = [UIFont boldSystemFontOfSize:16.0];
//    label.text = self.sectionNameList[section];
//    [view addSubview:label];
//    return view;
//}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self doViewPlan];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
